//
//  STAddCardViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "STAddCardViewController.h"
#define bankeDes @"请选择银行卡"
@interface STAddCardViewController ()<UITextFieldDelegate>
@property(nonatomic, strong) UITextField                     *zhihangTextField;
@property(nonatomic, strong) UITextField                     *nameTextField;
@property(nonatomic, strong) UITextField                     *idCardTextField;
@property(nonatomic, strong) STButton                        *bankButton;//银行
@property(nonatomic, strong) NSArray                         *bankArray;

@property(nonatomic, strong) NSArray <STTableEasyModel*>                    *datasouce;
@end

@implementation STAddCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加银行卡";
    [self st_setRightItemWithTitle:@"保存" titleColor:[UIColor whiteColor]];
    [self configSubView];
    self.nameTextField.delegate = self;
    self.zhihangTextField.userInteractionEnabled = NO;
    self.datasouce = @[
                       [[STTableEasyModel alloc]initWithTextString:@"提现账号" accessoryView:self.idCardTextField],
                       [[STTableEasyModel alloc]initWithTextString:@"提现姓名" accessoryView:self.nameTextField],
                       [[STTableEasyModel alloc]initWithTextString:@"提现银行" accessoryView:self.zhihangTextField]
                       ];
    [self addNotifacations];
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFiledDidChange:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)textFiledDidChange:(NSNotification*)notify{
    if (notify.object == self.idCardTextField) {
        //16 ~19
        if (self.idCardTextField.text.length >15 ) {
            [self sendWhichCarTypeWithAccount:self.idCardTextField.text];
        }
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    _bankButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 50)
                                            title:@"请绑定持卡人本人的银行卡"
                                       titleColor:FirstTextColor
                                        titleFont:15
                                     cornerRadius:0
                                  backgroundColor:self.tableView.backgroundColor
                                  backgroundImage:nil
                                            image:nil];
    [self.bankButton setTitleEdgeInsets:UIEdgeInsetsMake(-5, 15, 0, 0)];
    [self.bankButton setClicAction:^(UIButton *sender) {
        //[weakSelf onSelectedBankTypeButton];
    }];
    self.tableView.tableHeaderView = weakSelf.bankButton;
    
    self.zhihangTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)];
    self.zhihangTextField.keyboardType = UIKeyboardTypeDefault;
    self.zhihangTextField.placeholder = @"xx银行";
    self.zhihangTextField.textColor = FirstTextColor;
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)];
    self.nameTextField.keyboardType = UIKeyboardTypeDefault;
    self.nameTextField.placeholder = @"姓名";
    self.nameTextField.textColor = FirstTextColor;
    
    self.idCardTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth *0.6, 44)];
    self.idCardTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.idCardTextField.placeholder = @"银行卡卡号";
    self.idCardTextField.textColor = FirstTextColor;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datasouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    cell.textLabel.text = [NSString stringWithFormat:@"%lu",indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    cell.textLabel.text = self.datasouce[indexPath.row].textString;
    cell.accessoryView = self.datasouce[indexPath.row].accessoryView;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{
    
    if ([self.bankButton.currentTitle isEqualToString:bankeDes]) {
        [SVProgressHUD showErrorWithStatus:bankeDes];
        return;
    }
    if (!self.zhihangTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"银行卡号不正确"];
        return;
    }
    if (!self.nameTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入提现姓名"];
        return;
    }
    if (!self.idCardTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入提现账号"];
        return;
    }
    [self sendAddbankRequest];
    
}
- (void)sendAddbankRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (self.zhihangTextField.text.length) {
        [dic setObject:self.zhihangTextField.text forKey:@"bank"];
    }
    if (self.nameTextField.text.length) {
         [dic setObject:self.nameTextField.text forKey:@"name"];
    }
    if (self.idCardTextField.text.length) {
         [dic setObject:self.idCardTextField.text forKey:@"card_number"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/createbank",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD showSuccessWithStatus:@"添加银行卡成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                  [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                             }];
}

#pragma mark --NetWork Method
- (void)sendWhichCarTypeWithAccount:(NSString*)account{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 10;
    [dic setObject:@"687afe8de949e47ecc598b32362da336" forKey:@"key"];
    [dic setObject:account forKey:@"bankcard"];
    NSString * url = @"http://v.juhe.cn/bankcardinfo/query";
    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * dic = responseObject[@"result"];
        if ([dic isKindOfClass:[NSDictionary class]]) {
            if ([dic.allKeys containsObject:@"bank"]) {
                NSString * bank = dic[@"bank"];
                self.zhihangTextField.text = bank;
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}
@end
